package org.client.iot.ui.ble.adapter;

import android.net.wifi.p2p.WifiP2pDevice;
import android.widget.Button;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.clj.fastble.data.BleDevice;

import org.client.iot.R;
import org.client.iot.domain.WifiInfo;
import org.jetbrains.annotations.NotNull;

import java.util.List;

import cn.hutool.core.util.StrUtil;

public class ScanResultAdapter extends BaseQuickAdapter<WifiP2pDevice, BaseViewHolder> {

    private OnItemClickListener onItemClickListener;
    public ScanResultAdapter(List<WifiP2pDevice> wifiInfoList) {
        super(R.layout.content_adapter_scan_result, wifiInfoList);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, WifiP2pDevice item) {
        baseViewHolder.setText(R.id.textView_device_name, item.deviceName);
//        baseViewHolder.setText(R.id.textView_device_mac, "(" + item.getLevel() + "dBm)");
        baseViewHolder.setText(R.id.textView_device_mac, item.deviceAddress);
        Button btnBind = (Button)baseViewHolder.findView(R.id.btn_bind);
        btnBind.setOnClickListener(v -> {
            if(onItemClickListener!=null){
                onItemClickListener.onClick(item);
            }
        });
    }

    @Override
    public void addData(@NonNull @NotNull WifiP2pDevice data) {
        boolean isExs = false;
        List<WifiP2pDevice> wifiInfoList = this.getData();
        int index = wifiInfoList.size();
        for (int i = 0; i < wifiInfoList.size(); i++) {
            String address = wifiInfoList.get(i).deviceAddress;
            if(data.deviceAddress.equals(address)){
                isExs = true;
            }
        }
        if(!isExs) {
            super.addData(data);
        }
    }

    public void clearAll(){
        this.getData().clear();
        notifyDataSetChanged();
    }

    public interface OnItemClickListener{
        void onClick(WifiP2pDevice data);
    }
}
